//
//  myInformationView.m
//  Follow Me
//
//  Created by apple on 2017/4/7.
//  Copyright © 2017年 apple. All rights reserved.
//
#define AppRootViewController  ([[[[UIApplication sharedApplication] delegate] window] rootViewController])
#define takePhotoShare [takePhoto sharedModel]


#import "myInformationView.h"
#import "loginView.h"
#import "takePhoto.h"
#import "Users+CoreDataClass.h"
#import "Routes+CoreDataClass.h"
#import "Comments+CoreDataClass.h"
#import "MainModel.h"
#import "aboutView.h"


@interface myInformationView()<UIActionSheetDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate>

@property (nonatomic,strong)loginView *backToLogin;
@property (nonatomic,strong)aboutView *aboutInformation;
@end


@implementation myInformationView


-(void)awakeFromNib
{
    
    [super awakeFromNib];
    
    self.addUserPicture.layer.cornerRadius = 75;
    self.addUserPicture.layer.borderWidth = 0.7f;
    self.addUserPicture.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.addUserPicture.layer.masksToBounds = YES;
    self.myFollowRecord.layer.cornerRadius = 12;
    self.myFollowRecord.layer.borderWidth = 0.7f;
    self.myFollowRecord.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.muCommentRecord.layer.cornerRadius = 12;
    self.muCommentRecord.layer.borderWidth = 0.7f;
    self.muCommentRecord.layer.borderColor = [UIColor lightGrayColor].CGColor;
    


    NSArray *userExist = [Users findByAttribute:@"userName" withValue:[MainModel model].userMsg.userName];
    Users *ExistUserInfo = [userExist firstObject];
    UIImage *currentPNG =[UIImage imageWithData:ExistUserInfo.userPicture] ;
    [self.addUserPicture setImage:currentPNG forState:UIControlStateNormal];
}

- (IBAction)logOutAndBackToLoginView:(id)sender {
    
    self.backToLogin = [[NSBundle mainBundle]loadNibNamed:@"loginView" owner:self options:nil].firstObject;
    [self addSubview:self.backToLogin];
    

    
    
}

- (IBAction)addpicture:(id)sender {
    
    
    UIImagePickerController* imagePickerController = [[UIImagePickerController alloc] init];
    
        imagePickerController.delegate = self;
        imagePickerController.allowsEditing = YES;

    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
            {
        
                imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
        
            }
            else
            {
                imagePickerController.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        
            }
        
            [AppRootViewController presentViewController:imagePickerController animated:YES completion:NULL];

    
//    [takePhoto sharePicture:^(UIImage *HeadImage){
//        
//        [self.addUserPicture setBackgroundImage:HeadImage forState:UIControlStateNormal];
//        
//    }];
}





- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{

    [picker dismissViewControllerAnimated:YES completion:^{}];


    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];


    

    NSArray *user = [Users findByAttribute:@"userName" withValue:[MainModel model].userMsg.userName];
    Users *userInfo = [user firstObject];
    NSData *userImageData = UIImagePNGRepresentation(image);
    userInfo.userPicture = userImageData;
    [[NSManagedObjectContext defaultContext] saveOnlySelfWithCompletion:^(BOOL success, NSError *error) {
          NSLog(@"%@",userInfo);
        
    }];
    [self.addUserPicture setImage:image forState:UIControlStateNormal];
    [self.addUserPicture setTitle:@"" forState:UIControlStateNormal];
    
    
    
   
}

- (IBAction)showAboutView:(id)sender {
    if (!self.aboutInformation){
        self.aboutInformation = [[NSBundle mainBundle] loadNibNamed:@"aboutView" owner:self options:nil].firstObject;
        
    }
    [self addSubview:self.aboutInformation];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
